#!/bin/bash

# integration_test.sh - git_pull_all 脚本集成测试
#
# 用途: 集成测试 git_pull_all 脚本的完整功能
# 作者: Claude
# 版本: 1.0

set -euo pipefail

# 测试环境设置
readonly TEST_DIR="/tmp/git_pull_all_integration_test"
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../scripts" && pwd)"
readonly MAIN_SCRIPT="$SCRIPT_DIR/git_pull_all.sh"

# 颜色定义
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[0;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color

# 测试计数器
total_tests=0
passed_tests=0
failed_tests=0

# 初始化集成测试环境
setup_integration_test() {
    echo -e "${BLUE}设置集成测试环境...${NC}"
    
    # 创建测试目录
    mkdir -p "$TEST_DIR"
    cd "$TEST_DIR"
    
    # 创建实际的Git仓库
    create_real_git_repos
    
    # 复制脚本
    cp "$MAIN_SCRIPT" ./git_pull_all.sh
    
    echo -e "${GREEN}集成测试环境设置完成${NC}"
}

# 创建实际的Git仓库
create_real_git_repos() {
    echo "创建实际的Git仓库..."
    
    # 创建main分支仓库
    mkdir -p main_repo
    cd main_repo
    git init
    git config user.name "Test User"
    git config user.email "test@example.com"
    git branch -m main
    echo "# Main Repo" > README.md
    git add README.md
    git commit -m "Initial commit"
    git remote add origin https://github.com/test/main-repo.git
    cd ..
    
    # 创建master分支仓库
    mkdir -p master_repo
    cd master_repo
    git init
    git config user.name "Test User"
    git config user.email "test@example.com"
    git branch -m master
    echo "# Master Repo" > README.md
    git add README.md
    git commit -m "Initial commit"
    git remote add origin https://github.com/test/master-repo.git
    cd ..
    
    # 创建无远程仓库（模拟）
    mkdir -p local_repo
    cd local_repo
    git init
    git config user.name "Test User"
    git config user.email "test@example.com"
    git branch -m main
    echo "# Local Repo" > README.md
    git add README.md
    git commit -m "Initial commit"
    # 不设置远程仓库
    cd ..
    
    # 创建非Git目录
    mkdir -p non_git_dir
    
    echo "实际Git仓库创建完成"
}

# 清理测试环境
cleanup_integration_test() {
    echo -e "${BLUE}清理集成测试环境...${NC}"
    
    # 返回到原目录
    cd /
    
    # 删除测试目录
    rm -rf "$TEST_DIR"
    
    echo -e "${GREEN}集成测试环境清理完成${NC}"
}

# 测试结果输出
integration_test_result() {
    local test_name="$1"
    local expected="$2"
    local actual="$3"
    
    total_tests=$((total_tests + 1))
    
    if [[ "$expected" == "$actual" ]]; then
        echo -e "${GREEN}✓ ${test_name}${NC}"
        passed_tests=$((passed_tests + 1))
        return 0
    else
        echo -e "${RED}✗ ${test_name}${NC}"
        echo -e "  预期: $expected"
        echo -e "  实际: $actual"
        failed_tests=$((failed_tests + 1))
        return 1
    fi
}

# 测试脚本帮助信息
test_script_help() {
    echo -e "\n${BLUE}测试脚本帮助信息...${NC}"
    
    local help_output
    help_output=$(cd "$TEST_DIR" && ./git_pull_all.sh --help)
    
    if echo "$help_output" | grep -q "git_pull_all"; then
        integration_test_result "帮助信息包含脚本名称" "true" "true"
    else
        integration_test_result "帮助信息包含脚本名称" "true" "false"
    fi
    
    if echo "$help_output" | grep -q "批量拉取Git仓库"; then
        integration_test_result "帮助信息包含功能描述" "true" "true"
    else
        integration_test_result "帮助信息包含功能描述" "true" "false"
    fi
}

# 测试干运行模式
test_dry_run_mode() {
    echo -e "\n${BLUE}测试干运行模式...${NC}"
    
    local output
    output=$(cd "$TEST_DIR" && ./git_pull_all.sh --dry-run)
    
    if echo "$output" | grep -q "干运行模式"; then
        integration_test_result "干运行模式提示" "true" "true"
    else
        integration_test_result "干运行模式提示" "true" "false"
    fi
    
    if echo "$output" | grep -q "发现.*个Git仓库"; then
        integration_test_result "干运行模式检测仓库" "true" "true"
    else
        integration_test_result "干运行模式检测仓库" "true" "false"
    fi
}

# 测试详细输出模式
test_verbose_mode() {
    echo -e "\n${BLUE}测试详细输出模式...${NC}"
    
    local output
    output=$(cd "$TEST_DIR" && ./git_pull_all.sh --dry-run -v)
    
    if echo "$output" | grep -q "开始扫描"; then
        integration_test_result "详细输出包含扫描信息" "true" "true"
    else
        integration_test_result "详细输出包含扫描信息" "true" "false"
    fi
    
    if echo "$output" | grep -q "DEBUG"; then
        integration_test_result "详细输出包含调试信息" "true" "true"
    else
        integration_test_result "详细输出包含调试信息" "true" "false"
    fi
}

# 测试静默模式
test_quiet_mode() {
    echo -e "\n${Blue}测试静默模式...${NC}"
    
    local output
    output=$(cd "$TEST_DIR" && ./git_pull_all.sh --dry-run -q 2>&1)
    
    if [[ -z "$output" ]]; then
        integration_test_result "静默模式无输出" "true" "true"
    else
        integration_test_result "静默模式无输出" "true" "false"
    fi
}

# 测试目录扫描功能
test_directory_scanning() {
    echo -e "\n${BLUE}测试目录扫描功能...${NC}"
    
    local output
    output=$(cd "$TEST_DIR" && ./git_pull_all.sh --dry-run)
    
    if echo "$output" | grep -q "main_repo"; then
        integration_test_result "扫描到main_repo" "true" "true"
    else
        integration_test_result "扫描到main_repo" "true" "false"
    fi
    
    if echo "$output" | grep -q "master_repo"; then
        integration_test_result "扫描到master_repo" "true" "true"
    else
        integration_test_result "扫描到master_repo" "true" "false"
    fi
    
    if echo "$output" | grep -q "local_repo"; then
        integration_test_result "扫描到local_repo" "true" "true"
    else
        integration_test_result "扫描到local_repo" "true" "false"
    fi
    
    if echo "$output" | grep -q "non_git_dir"; then
        integration_test_result "跳过非Git目录" "true" "true"
    else
        integration_test_result "跳过非Git目录" "true" "false"
    fi
}

# 测试分支检测
test_branch_detection() {
    echo -e "\n${BLUE}测试分支检测...${NC}"
    
    local output
    output=$(cd "$TEST_DIR" && ./git_pull_all.sh --dry-run)
    
    if echo "$output" | grep -q "origin/main"; then
        integration_test_result "检测到main分支" "true" "true"
    else
        integration_test_result "检测到main分支" "true" "false"
    fi
    
    if echo "$output" | grep -q "origin/master"; then
        integration_test_result "检测到master分支" "true" "true"
    else
        integration_test_result "检测到master分支" "true" "false"
    fi
}

# 测试错误处理
test_error_handling() {
    echo -e "\n${BLUE}测试错误处理...${NC}"
    
    # 测试不存在的目录
    local output
    output=$(cd "$TEST_DIR" && ./git_pull_all.sh /non/existent/path 2>&1 || echo "ERROR")
    
    if echo "$output" | grep -q "目录不存在"; then
        integration_test_result "处理不存在目录" "true" "true"
    else
        integration_test_result "处理不存在目录" "true" "false"
    fi
    
    # 测试空目录
    mkdir -p empty_dir
    output=$(cd "$TEST_DIR" && ./git_pull_all.sh empty_dir 2>&1 || echo "ERROR")
    
    if echo "$output" | grep -q "未发现任何子目录"; then
        integration_test_result "处理空目录" "true" "true"
    else
        integration_test_result "处理空目录" "true" "false"
    fi
}

# 运行所有集成测试
run_all_integration_tests() {
    echo -e "${BLUE}开始运行所有集成测试...${NC}"
    echo "=================================="
    
    setup_integration_test
    
    # 运行各个测试函数
    test_script_help
    test_dry_run_mode
    test_verbose_mode
    test_quiet_mode
    test_directory_scanning
    test_branch_detection
    test_error_handling
    
    cleanup_integration_test
    
    # 输出测试结果汇总
    echo -e "\n${BLUE}集成测试结果汇总${NC}"
    echo "=================================="
    echo "总测试数: $total_tests"
    echo -e "${GREEN}通过: $passed_tests${NC}"
    echo -e "${RED}失败: $failed_tests${NC}"
    
    if [[ $failed_tests -eq 0 ]]; then
        echo -e "${GREEN}所有集成测试通过！${NC}"
        exit 0
    else
        echo -e "${RED}有 $failed_tests 个集成测试失败${NC}"
        exit 1
    fi
}

# 显示测试帮助信息
show_help() {
    cat << EOF
integration_test.sh - git_pull_all 脚本集成测试

用法: $0 [选项]

选项:
    -h, --help      显示此帮助信息
    --run-tests     运行所有集成测试

示例:
    $0 --run-tests    运行所有集成测试并显示结果
EOF
}

# 主函数
main() {
    case "${1:-}" in
        -h|--help)
            show_help
            exit 0
            ;;
        --run-tests|"")
            run_all_integration_tests
            ;;
        *)
            echo "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
}

main "$@"